package utils.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.RecommendDetailsEntity;
import tools.TimeUtil;
import utils.texttools.StringUtil;

/**
 * Created by dllo on 15/11/26.
 */
public class RecomendListAdpter extends BaseAdapter {
    private LayoutInflater inflater;

    private List<RecommendDetailsEntity.TracksEntity.ListEntity> listEntity;


    public void addData(List<RecommendDetailsEntity.TracksEntity.ListEntity> listEntity) {
        this.listEntity = listEntity;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {

        return listEntity != null && listEntity.size() > 0 ? listEntity.size() : 0;
    }

    //
    @Override
    public Object getItem(int position) {

        return listEntity.get(position);
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    //加载布局
    @Override
    public View getView(int position, View v, ViewGroup parent) {

        if (listEntity != null && listEntity.size() > 0) {
            ViewHolder holder = null;
            if (v == null) {
                holder = new ViewHolder();
                //加载布局
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                v = inflater.inflate(R.layout.fragment_listview_recommed, null);
                //初始化控件
                holder.list_title = (TextView) v.findViewById(R.id.list_title);
                holder.reListViewPlayNum = (TextView) v.findViewById(R.id.reListViewPlayNum);
                holder.reListViewTime = (TextView) v.findViewById(R.id.reListViewTime);
                holder.reListViewHf = (TextView) v.findViewById(R.id.reListViewHf);
                holder.reListViewCreatAt = (TextView) v.findViewById(R.id.reListViewCreatAt);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }
            holder.list_title.setText(listEntity.get(position).getTitle());
            holder.reListViewPlayNum.setText(StringUtil.longToWan(listEntity.get(position).getPlaytimes()));
            holder.reListViewTime.setText(StringUtil.longToWan(listEntity.get(position).getDownloadAacSize()));
            holder.reListViewHf.setText(StringUtil.longToWan(listEntity.get(position).getComments()));
            holder.reListViewCreatAt.setText(TimeUtil.getDescriptionTimeFromTimestamp(listEntity.get(position).getCreatedAt()));
        }
        return v;
    }

    class ViewHolder {
        private TextView list_title, reListViewPlayNum, reListViewTime, reListViewHf, reListViewCreatAt;
    }

}
